package client.ui.controllers;

/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * GameScreen.java
 *
 * Steven Louie Apr. 8, 2012
 */
import client.ActinoClient;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.screen.KeyInputHandler;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class GameScreen implements ScreenController {

    private Element chatInput;
    private Nifty nifty;

    @Override
    public void bind(Nifty nifty, Screen screen) {
        this.nifty = nifty;
        chatInput = screen.findElementByName("chatinput");
    }

    @Override
    public void onStartScreen() {
        chatInput.addInputHandler(new KeyInputHandler() {

            @Override
            public boolean keyEvent(NiftyInputEvent inputEvent) {
                if (inputEvent == null) {
                    return false;
                }
                switch (inputEvent) {
                    case SubmitText:
                        sendMessage();
                        return true;
                }
                return false;
            }
        });
    }

    @Override
    public void onEndScreen() {
    }

    public void sendMessage() {
        TextField msg = nifty.getCurrentScreen().findNiftyControl("chatinput", TextField.class);
        String s = msg.getText();
        if (s != null && !s.trim().isEmpty()) {
            ActinoClient.getInstance().getTcp().sendChatMessage(s);
        }
        msg.setText("");
    }

}
